package com.oceansoft.mobile.econsole.exam.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oceansoft.mobile.econsole.common.util.StringUtils;
import com.oceansoft.mobile.econsole.common.util.web.PageUtil;
import com.oceansoft.mobile.econsole.exam.entity.Attach;
import com.oceansoft.mobile.econsole.exam.entity.ExamTest;
import com.oceansoft.mobile.econsole.exam.entity.Option;
import com.oceansoft.mobile.econsole.exam.entity.Questions;
import com.oceansoft.mobile.econsole.exam.persistence.IExamMapper;
import com.oceansoft.mobile.econsole.exam.service.IExamService;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**

 * Created by 祝亚 on 2016/8/17.
 */
@Service
public class ExamService implements IExamService{
    @Resource
    private IExamMapper examMapper;
    @Override
    public Pager<ExamTest> findTestByCondition(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<ExamTest> page = (Page<ExamTest>) examMapper.findTestByCondition(query);
        return PageUtil.page2pager(page, query);
    }

    @Override
    @Transactional
    public Result saveOrUpdateExamTest(ExamTest examTest)  throws Exception{
        if(examTest.getId()==Integer.MIN_VALUE){
            examMapper.addExamTest(examTest);
        }else{
            examMapper.updateExamTest(examTest);
        }

        return new Result();
    }

    @Override
    @Transactional
    public Result deleteExamTest(int id) throws Exception{
        examMapper.deleteExamTest(id);
        return new Result();
    }

    @Override
    public ExamTest getExamTestById(int id) {
        return examMapper.getExamTestById(id);
    }

    @Override
    public Pager<Questions> findQuestionByCondition(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<Questions> page = (Page<Questions>) examMapper.findQuestionByCondition(query);
        return PageUtil.page2pager(page, query);
    }

    @Override
    public Questions getQuestionById(int id) {
        return examMapper.getQuestionById(id);
    }

    @Override
    public List<Option> findOptionByQuestionId(int id) {
        List<Option> list =examMapper.findOptionByQuestionId(id);
        for(int i=0;i<list.size();i++){
            List<Attach> attach = examMapper.findAttachByQuestionId(list.get(i).getId(),"2");
            list.get(i).setAttach(attach);
        }
        return list;
    }

    @Override
    public List<Attach> findAttachByQuestionId(int id,String type) {
        return examMapper.findAttachByQuestionId(id, type);
    }

    @Override
    public Result saveQuestions(Questions questions, MultipartHttpServletRequest request) throws Exception{
        //保存题目信息
        if(questions.getId()==0){
            examMapper.saveQuestions(questions);
        }else{//编辑题目
            examMapper.updateQuestion(questions);
        }
        MultipartFile questionImg =request.getFile("questionAttach");

        if(null!=questionImg){
            //保存题目图片
            Attach attach = new Attach();
            attach.setName(questionImg.getOriginalFilename());
            attach.setQuestionid(questions.getId());
            attach.setType("1");
            attach.setContent(questionImg.getBytes());
            examMapper.saveAttach(attach);
            questions.setIsimg("1");
        }
        int count=0;
        if(StringUtils.isNotEmpty(request.getParameter("count"))){
            count = Integer.parseInt(request.getParameter("count"));
        }
        int label=1;
        String answer="";
        for(int i=1;i<=count;i++){
            if(StringUtils.isNotEmpty(request.getParameter("name"+i))){
                //保存选项
                MultipartFile img =request.getFile("imgattach"+i);
                Option o = new Option();
                o.setQuestionid(questions.getId());
                o.setName(request.getParameter("name" + i));
                o.setLabel(labelMap.get(label));
                examMapper.saveOption(o);
                if("1".equals(request.getParameter("isanswer"+i))){
                    if(answer.length()>0){
                        answer+=","+ labelMap.get(label);
                    }else{
                        answer+= labelMap.get(label);
                    }
                }
                if(null!=img&&img.getSize()>0){
                    //保存选项图片
                    Attach attach = new Attach();
                    attach.setName(img.getOriginalFilename());
                    attach.setQuestionid(o.getId());
                    attach.setType("2");
                    attach.setContent(img.getBytes());
                    examMapper.saveAttach(attach);
                    questions.setIsoptionimg("1");
                }
                label++;
            }
            questions.setAnswer(answer);
            //保存题目答案
            if(StringUtils.isNotEmpty(answer)){
                examMapper.updateQuestion(questions);
            }

        }
        return new Result();
    }

    @Override
    public List<ExamTest> findAllTest() {
        return examMapper.findAllTest();
    }


    @Override
    public Attach getAttachById(int id) {
        return examMapper.getAttachById(id);
    }

    /**
     * 短信发送业务类型字典
     */
    public static final HashMap<Integer, String> labelMap = new HashMap<Integer, String>() {
        {
            put(1, "A");
            put(2, "B");
            put(3, "C");
            put(4, "D");
            put(5, "E");
            put(6, "F");
            put(7, "G");
            put(8, "H");
            put(9, "I");
            put(10, "J");
            put(11, "K");
            put(12, "L");
            put(13, "M");
            put(14, "N");
            put(15, "O");
            put(16, "P");
            put(17, "Q");
            put(18, "R");
            put(19, "S");
            put(20, "T");
            put(21, "U");
            put(22, "V");
            put(23, "W");
            put(24, "X");
            put(25, "Y");
            put(26, "Z");
        }
    };

}
